package com.iqiyi.game.ams.infrastructure.repository;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.iqiyi.game.ams.infrastructure.app.AppException;
import com.iqiyi.game.ams.domain.acitivty.entity.Activity;
import com.iqiyi.game.ams.domain.acitivty.entity.Module;
import com.iqiyi.game.ams.infrastructure.dao.ActivityDAO;
import com.iqiyi.game.ams.infrastructure.dao.ActivityModuleDAO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author hanbaoyu
 */
@Component
public class ActivityRepository {

    @Autowired
    ActivityDAO activityDAO;

    @Autowired
    ActivityModuleDAO activityModuleDAO;

    /**
     * @param id
     * @return
     */
    public Activity of(Integer id) {

        // 加缓存
        ActivityDAO activityData = activityDAO.selectById(id);
        if (activityData == null) {
            throw new AppException("活动不存在");
        }

        Activity activity = new Activity();
        BeanUtils.copyProperties(activityData, activity);

        // 查询活动模块
        LambdaQueryWrapper<ActivityModuleDAO> lambdaQueryActivityModule = Wrappers.<ActivityModuleDAO>lambdaQuery();
        lambdaQueryActivityModule.eq(ActivityModuleDAO::getActivityId, id);
        List<ActivityModuleDAO> activityModulesDataList =  activityModuleDAO.selectList(lambdaQueryActivityModule);

        activityModulesDataList.forEach(v -> {
            Module module = new Module();
            BeanUtils.copyProperties(v, module);
            activity.getModuleMap().put(module.getLabel(), module);
        });

        return activity;
    }

}
